package com.zcn.io.web.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zcn.io.common.entity.TrusteeshipDetail;
import com.zcn.io.common.exception.ServiceException;
import com.zcn.io.common.util.DateUtil;
import com.zcn.io.web.api.vo.TrusteeshipDetailVo;
import com.zcn.io.web.api.entity.Maintenance;
import com.zcn.io.web.mapper.TrusteeshipDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @desc:
 * @author: 202438 邱建伟
 * @date: 2024/10/2 15:48
 * @Copyright (c) 2024, 江苏金恒信息科技股份有限公司
 */
@Service
@RequiredArgsConstructor
public class TrusteeshipDetailService {

    private final TrusteeshipDetailMapper trusteeshipDetailMapper;

//    public void list(Page<TrusteeshipDetailVo> page, TrusteeshipDetailQuery query) {
//        trusteeshipDetailMapper.list(page, query);
//    }



    public void save(TrusteeshipDetailVo trusteeshipDetail) {
        //唯一性校验
        LambdaQueryWrapper<TrusteeshipDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrusteeshipDetail::getOrderId, trusteeshipDetail.getOrderId());
        queryWrapper.eq(TrusteeshipDetail::getPlatform, trusteeshipDetail.getPlatform());
        List<TrusteeshipDetail> trusteeshipDetails = trusteeshipDetailMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(trusteeshipDetails)) {
            throw new ServiceException(600, "已存在该平台记录!");
        }
        if (StrUtil.equals(trusteeshipDetail.getStatusChange(),"1")){
            trusteeshipDetail.setLastHandleTime(DateUtil.getDefaultDate());
        }
        trusteeshipDetail.setCreateDateTime(DateUtil.getDefaultDate());
        trusteeshipDetailMapper.insert(trusteeshipDetail);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updateById(TrusteeshipDetailVo trusteeshipDetail) {
//        if (StrUtil.equals(trusteeshipDetail.getStatusChange(), "1")) {
//            trusteeshipDetail.setLastHandleTime(DateUtil.getDefaultDate());
//        }
        //更新托管明细信息
        updateTrusteeshipDetail(trusteeshipDetail);
        //更新维护信息
        updateMaintenanceInfo(trusteeshipDetail);

    }

    public void updateTrusteeshipDetail(TrusteeshipDetailVo trusteeshipDetail) {
        trusteeshipDetailMapper.updateTrusteeshipDetail(trusteeshipDetail);
    }

    public void updateMaintenanceInfo(TrusteeshipDetailVo trusteeshipDetail) {
        trusteeshipDetail.setLastHandleTime(DateUtil.getDefaultDate());
        trusteeshipDetailMapper.updateMaintenanceInfo(trusteeshipDetail);
    }

    public void deleteById(String id) {
        trusteeshipDetailMapper.deleteById(id);
    }

    public List<TrusteeshipDetailVo> listAllByOrderId(String orderId) {
        return this.trusteeshipDetailMapper.listAllByOrderId(orderId);
    }

    public TrusteeshipDetail getById(String id) {
        return this.trusteeshipDetailMapper.selectById(id);
    }

    public void saveInfo(TrusteeshipDetail trusteeshipDetail) {
        this.trusteeshipDetailMapper.saveInfo(trusteeshipDetail);
    }

    /**
     * @desc: 将最新一次的维护订单新更新到托管详情中
     * @author 202438 邱建伟
     * @date 2025/5/1 16:26
     */
    public void updateMaintenanceLatestId(Maintenance maintenance) {
        this.trusteeshipDetailMapper.updateMaintenanceLatestId(maintenance);
    }

    /**
     * @desc: 更新托管明细中的资料信息
     * @author 202438 邱建伟
     * @date 2025/5/2 19:07
     */
    public void updateInfo(TrusteeshipDetailVo trusteeshipDetailVo) {
        this.trusteeshipDetailMapper.updateInfo(trusteeshipDetailVo);
    }

    public void updateMaintenanceLatestIdByTgDtlId(String tgDtlId) {
        this.trusteeshipDetailMapper.updateMaintenanceLatestIdByTgDtlId(tgDtlId);
    }
}
